//
//  DSLearnScoreViewModel.m
//  DSJY
//
//  Created by 冯华强 on 16/9/28.
//  Copyright © 2016年 dsjy. All rights reserved.
//  我的成绩VM

#import "DSLearnScoreViewModel.h"
#import "DSLearnScore.h"
#import "DSNetworkTool.h"
#import "DSAccountViewModel.h"
#import "DSLearnStatis.h"
#import "YQResponse.h"

@interface DSLearnScoreViewModel ()
//学习记录列表
@property (nonatomic, strong, readwrite) NSMutableArray<DSLearnScore *> *learnScoreList;
//我的成绩
@property (nonatomic, strong, readwrite) DSLearnStatis *myLearnStatis;
@end

@implementation DSLearnScoreViewModel

#pragma mark - 懒加载
- (NSMutableArray<DSLearnScore *> *)learnScoreList
{
    if (_learnScoreList == nil) {
        _learnScoreList = [NSMutableArray<DSLearnScore *> array];
    }
    return _learnScoreList;
}

#pragma mark - 初始化
- (instancetype)init
{
    if (self = [super init]) {
        //绑定事件
        [self initialBind];
    }
    return self;
}

- (void)initialBind
{
    @weakify(self);
    //初始化数据
    [self createCmdSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self);
        [DSNetworkTool learnInfoListWithParams:[DSAccountViewModel sharedAccount].account pagenum:self.pageNum pagecount:self.pageCount success:^(NSDictionary *responseObject) {
            //返回值
            self.response = [YQResponse mj_objectWithKeyValues:responseObject];
            [self veriResponse:self.response finishedCallBack:^{
                //列表
                NSArray<DSLearnScore *> *learnScoreList = [DSLearnScore mj_objectArrayWithKeyValuesArray:responseObject[@"datas"]];
                //我的成绩
                self.myLearnStatis = [DSLearnStatis mj_objectWithKeyValues:responseObject[@"attrs"]];
                
                if (self.pageNum == 1) {
                    [self.learnScoreList removeAllObjects];
                }
                [self.learnScoreList addObjectsFromArray:learnScoreList];
                
                [subscriber sendNext:nil];
            }];
            [subscriber sendCompleted];
        } failure:^(NSError *error) {
            MTLog(@"%@", error);
            [SVProgressHUD showErrorWithStatus:@"系统异常!"];
            [subscriber sendNext:error];
            [subscriber sendCompleted];
        }];
        return nil;
    }];
    
    //设置请求时的提示框
    [self executingWithCommand:self.requestCommand];
}


@end
